package cn.edu.cdu.health.common;

import lombok.Data;

import java.io.Serializable;

// 设计一个统一的给前端的返回类型
//
/*{
    "code":200,
    "msg":"success",
    "data":{
            "id":1,
            "name":"zhang"
        }
    }*/

@Data
public class R<T> implements Serializable {

    private Integer code;
    private String msg;
    private Object data;

    /**
     * 请求成功的方法
     * @return
     */
    public static R ok(){
        R r = new R();
        r.code = 200;
        r.msg = "ok";
        return r;
    }

    /**
     * 请求成功，并返回数据给前端
     * @param data
     * @return
     */
    public static R ok(Object data){
        R r = ok();
        r.data = data;
        return r;
    }

    public static R failed(){
        R r = new R();
        r.code = 500;
        r.msg = "failed";
        return r;
    }

    public static R failed(String msg){
        R r = new R();
        r.code = 500;
        r.msg = msg;
        return r;
    }
}
